package com.dmx.cep.action.epgManage;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.dmx.cep.entity.Channel;
import com.dmx.cep.entity.TvContentInfo;
import com.dmx.cep.entity.TvContentStatus;
import com.dmx.cep.entity.TvContentType;
import com.dmx.cep.service.EpgService;
import com.dmx.fw.util.DateTimeUtils;
import com.dmx.fw.util.DateTools;
import com.dmx.fw.web.struts2.action.CommonQueryAction;


public class EpgManageAction extends CommonQueryAction{
	
	private EpgService epgService;
	
	private String q_channelCode;
	
	private String  q_name;
	
	private String q_status;
	
	private String q_startDate=DateTools.date2String(new Date(),"yyyy-MM-dd");;
	
	private String q_endDate=DateTools.date2String(new Date(),"yyyy-MM-dd");;
	
	private String programId;
	
	private TvContentInfo program;
	
	private List<TvContentType> contentTypeList;

	public List<TvContentType> getContentTypeList() {
		return contentTypeList;
	}

	public void setContentTypeList(List<TvContentType> contentTypeList) {
		this.contentTypeList = contentTypeList;
	}


	public String getQ_channelCode() {
		return q_channelCode;
	}

	public void setQ_channelCode(String code) {
		q_channelCode = code;
	}

	public String getQ_name() {
		return q_name;
	}

	public void setQ_name(String q_name) {
		this.q_name = q_name;
	}

	public EpgService getEpgService() {
		return epgService;
	}

	public void setEpgService(EpgService epgService) {
		this.epgService = epgService;
	}
	
	
	public String execute() throws Exception{

        if("main".equals(this.act)){
        	return main();
        }	
		
        if("editInput".equals(this.act)){
        	return doEditInput();
        }
        
        if("editSubmit".equals(this.act)){
        	return doEditSubmit();
        }
        
        if("delete".equals(this.act)){
        	return doDelete();
        }
        
        if("loadXmlTree".equals(this.act)){
        	return loadXmlTree();
        }
		
		this.busiId="searchEPG";
		
		this._extraParamsMap.put("q_startDate", q_startDate);
		this._extraParamsMap.put("q_endDate", q_endDate);
		if(this.sortField==null||"".equals(this.sortField.trim())){
			this.sortField="START_TIME";
			this.sortMode="asc";
		}
		
		String result=super.execute();
		List searchResult=this.pageInfo.getSearchResult();
		for(int i=0;searchResult!=null&&i<searchResult.size();i++){
			List obj=(List)searchResult.get(i);
			String epgStarttime=obj.get(4).toString();
			Date startTime=DateTimeUtils.StringToDate(epgStarttime,"yyyy-MM-dd HH:mm:ss");
			if(startTime!=null&&startTime.after(new Date())){
				obj.set(7, "1");   //标志该节目是否开播或者过期
			}else{
				obj.set(7, "0");  //out of date
			}
		}
		
		return result;
	}

	//进入EPG管理
	public String main() throws Exception{
		
		return "main";
	}
	
	//进入修改输入界面
	public String doEditInput() throws Exception{
		
		if(program==null){
			
			program=(TvContentInfo)epgService.getObject(TvContentInfo.class, programId);
			
		}
		
		contentTypeList=epgService.getContentTypeList();
		
		return INPUT;
	}
	
	//进入修改输入界面
	public String doEditSubmit() throws Exception{

		try {
			
			TvContentInfo contentInfo=(TvContentInfo)epgService.getObject(TvContentInfo.class, program.getContentId());
			
			contentInfo.setTitle(program.getTitle());
			
			//contentInfo.setContentType(program.getContentType());
			
			contentInfo.setCategory(program.getCategory());
			
			contentInfo.setDescription(program.getDescription());
			
			setContentCategory(program.getCategory(),contentInfo);
			
			epgService.saveObject(contentInfo);
			
			this.setSuccessinfo(this.getText("info.modifySuccess"));
			
		} catch (Exception e) {
			log.error(e);
			this.setErrorInfo(this.getText("err.system.UnknownException"));
			return doEditInput();
		}
		
		return "list";
	}
	
	//重新删除
	public String doDelete() throws Exception{

		try {
			TvContentStatus contentInfoStatus=(TvContentStatus)epgService.getObject(TvContentStatus.class, programId);
			
			if(contentInfoStatus.getIsValid()==-1){  //删除失败
				contentInfoStatus.setIsValid(-3);  //请求重删
				
				epgService.saveObject(contentInfoStatus);
				
				this.setSuccessinfo(this.getText("info.operate.success"));
			}else{
				throw new RuntimeException("delete epg failed.");
			}
		} catch (Exception e) {
			log.error(e);
			this.setErrorInfo(this.getText("err.system.UnknownException"));
		}
		
		return "list";
	}	
	
	public String loadXmlTree() throws Exception{
		
		List<Channel> channelList=epgService.getChannelList();
		
		String xmlString=createXml("0",channelList);
		HttpServletResponse res = ServletActionContext.getResponse();
		res.setContentType("text/xml;charset=UTF-8");
		res.setHeader("Cache-Control", "no-store");
		res.setHeader("Pragma", "no-cache");
		res.setDateHeader("Expires", 0);
		PrintWriter out = res.getWriter();
		out.print(xmlString);
		out.flush();
		out.close();
		
		return null;
	}
	
	/**
	 * @Description: 动态生成树
	 * @param parentId
	 * @param tmpList
	 * @return String
	 */
	private String createXml(String parentId, List<Channel> channelList) {
		StringBuffer outXml = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
		outXml.append("<tree open=\"1\" id=\"");
		outXml.append(parentId);
		outXml.append("\">\n");
		for (Channel channel:channelList) {
			outXml.append("<item text=\"");
			outXml.append(channel.getCode());
			outXml.append("\" id=\"");
			outXml.append(channel.getCode());
			outXml
					.append("\" im0=\"page.gif\" im1=\"page.gif\" im2=\"page.gif\"");

			outXml.append(" child=\"1\"");

			outXml.append("></item>\n");
		}
		outXml.append("</tree>\n");
		return outXml.toString();
	}

	public TvContentInfo getProgram() {
		return program;
	}

	public void setProgram(TvContentInfo program) {
		this.program = program;
	}

	public String getProgramId() {
		return programId;
	}

	public void setProgramId(String programId) {
		this.programId = programId;
	}
	
	
	private void setContentCategory(String category,TvContentInfo tvContent){
		tvContent.setCategorySf(0);
		tvContent.setCategoryHorror(0);
		tvContent.setCategoryAction(0);
		tvContent.setCategorySuspense(0);
		tvContent.setCategoryComedy(0);
		tvContent.setCategoryLove(0);
		tvContent.setCategoryPeriod(0);
		tvContent.setCategoryNews(0);
		tvContent.setCategorySports(0);
		tvContent.setCategoryDrama(0);
		tvContent.setCategoryVariety(0);
		tvContent.setCategoryCook(0);
		tvContent.setCategoryQuiz(0);
		tvContent.setCategoryAnimation(0);
		tvContent.setCategoryMusic(0);
		tvContent.setCategoryKids(0);
		tvContent.setCategoryKnowledge(0);
		tvContent.setCategoryNewsComments(0);
		tvContent.setCategoryDocumentary(0);
		tvContent.setCategoryFinancial(0);
		tvContent.setCategoryEducation(0);
		tvContent.setCategoryForeignLanguage(0);
		tvContent.setCategoryEntertainment(0);
		tvContent.setCategorySeries(0);
		tvContent.setCategoryMovies(0);
		tvContent.setCategoryScience(0);
		tvContent.setCategoryLegal(0);
		tvContent.setCategoryMilitary(0);
		tvContent.setCategoryElderly(0);
		tvContent.setCategoryChildren(0);
		tvContent.setCategoryEnvironmental(0);
		tvContent.setCategorySocial(0);
		tvContent.setCategoryFashion(0);
		tvContent.setCategoryWeather(0);
		tvContent.setCategoryAdvertising(0);
		tvContent.setCategoryGangster(0);
		tvContent.setCategoryDisaster(0);
		tvContent.setCategoryEthics(0);
		tvContent.setCategoryBiography(0);
		tvContent.setCategoryMartial(0);
		tvContent.setCategoryWar(0);
		tvContent.setCategoryPolitical(0);
		tvContent.setCategoryDetective(0);
		tvContent.setCategoryLegendary(0);
		tvContent.setCategoryFantastic(0);
		tvContent.setCategoryOther(0);
		
		
		if("Sf".equalsIgnoreCase(category)){
			tvContent.setCategorySf(1);
		}
		else if("Horror".equalsIgnoreCase(category)){
			tvContent.setCategoryHorror(1);
		}
		else if("Action".equalsIgnoreCase(category)){
			tvContent.setCategoryAction(1);
		}
		else if("Suspense".equalsIgnoreCase(category)){
			tvContent.setCategorySuspense(1);
		}
		else if("Comedy".equalsIgnoreCase(category)){
			tvContent.setCategoryComedy(1);
		}
		else if("Love".equalsIgnoreCase(category)){
			tvContent.setCategoryLove(1);
		}
		else if("Period".equalsIgnoreCase(category)){
			tvContent.setCategoryPeriod(1);
		}
		else if("News".equalsIgnoreCase(category)){
			tvContent.setCategoryNews(1);
		}
		else if("Sports".equalsIgnoreCase(category)){
			tvContent.setCategorySports(1);
		}
		else if("Drama".equalsIgnoreCase(category)){
			tvContent.setCategoryDrama(1);
		}
		else if("Variety".equalsIgnoreCase(category)){
			tvContent.setCategoryVariety(1);
		}
		else if("Cook".equalsIgnoreCase(category)){
			tvContent.setCategoryCook(1);
		}
		else if("Quiz".equalsIgnoreCase(category)){
			tvContent.setCategoryQuiz(1);
		}
		else if("Animation".equalsIgnoreCase(category)){
			tvContent.setCategoryAnimation(1);
		}
		else if("Music".equalsIgnoreCase(category)){
			tvContent.setCategoryMusic(1);
		}
		else if("Kids".equalsIgnoreCase(category)){
			tvContent.setCategoryKids(1);
		}
		else if("Knowledge".equalsIgnoreCase(category)){
			tvContent.setCategoryKnowledge(1);
		}
		else if("News_comments".equalsIgnoreCase(category)){
			tvContent.setCategoryNewsComments(1);
		}
		else if("Documentary".equalsIgnoreCase(category)){
			tvContent.setCategoryDocumentary(1);
		}
		else if("Financial".equalsIgnoreCase(category)){
			tvContent.setCategoryFinancial(1);
		}
		else if("Education".equalsIgnoreCase(category)){
			tvContent.setCategoryEducation(1);
		}
		else if("Foreign_lang".equalsIgnoreCase(category)){
			tvContent.setCategoryForeignLanguage(1);
		}
		else if("Entertainment".equalsIgnoreCase(category)){
			tvContent.setCategoryEntertainment(1);
		}
		else if("Series".equalsIgnoreCase(category)){
			tvContent.setCategorySeries(1);
		}
		else if("Movies".equalsIgnoreCase(category)){
			tvContent.setCategoryMovies(1);
		}
		else if("Science".equalsIgnoreCase(category)){
			tvContent.setCategoryScience(1);
		}
		else if("Legal".equalsIgnoreCase(category)){
			tvContent.setCategoryLegal(1);
		}
		else if("Military".equalsIgnoreCase(category)){
			tvContent.setCategoryMilitary(1);
		}
		else if("Women".equalsIgnoreCase(category)){
			tvContent.setCategoryWomen(1);
		}
		else if("Elderly".equalsIgnoreCase(category)){
			tvContent.setCategoryElderly(1);
		}
		else if("Children".equalsIgnoreCase(category)){
			tvContent.setCategoryChildren(1);
		}
		else if("Environmental".equalsIgnoreCase(category)){
			tvContent.setCategoryEnvironmental(1);
		}
		else if("Social".equalsIgnoreCase(category)){
			tvContent.setCategorySocial(1);
		}
		else if("Fashion".equalsIgnoreCase(category)){
			tvContent.setCategoryFashion(1);
		}
		else if("Weather".equalsIgnoreCase(category)){
			tvContent.setCategoryWeather(1);
		}
		else if("Advertising".equalsIgnoreCase(category)){
			tvContent.setCategoryAdvertising(1);
		}
		else if("Gangster".equalsIgnoreCase(category)){
			tvContent.setCategoryGangster(1);
		}
		else if("Disaster".equalsIgnoreCase(category)){
			tvContent.setCategoryDisaster(1);
		}
		else if("Ethics".equalsIgnoreCase(category)){
			tvContent.setCategoryEthics(1);
		}
		else if("Biography".equalsIgnoreCase(category)){
			tvContent.setCategoryBiography(1);
		}
		else if("Martial".equalsIgnoreCase(category)){
			tvContent.setCategoryMartial(1);
		}
		else if("War".equalsIgnoreCase(category)){
			tvContent.setCategoryWar(1);
		}
		else if("Political".equalsIgnoreCase(category)){
			tvContent.setCategoryPolitical(1);
		}
		else if("Detective".equalsIgnoreCase(category)){
			tvContent.setCategoryDetective(1);
		}
		else if("Legendary".equalsIgnoreCase(category)){
			tvContent.setCategoryLegendary(1);
		}
		else if("Fantastic".equalsIgnoreCase(category)){
			tvContent.setCategoryFantastic(1);
		}
		else{
			tvContent.setCategoryOther(1);
		}
	}

	public String getQ_status() {
		return q_status;
	}

	public void setQ_status(String q_status) {
		this.q_status = q_status;
	}

	public String getQ_startDate() {
		return q_startDate;
	}

	public void setQ_startDate(String date) {
		q_startDate = date;
	}

	public String getQ_endDate() {
		return q_endDate;
	}

	public void setQ_endDate(String date) {
		q_endDate = date;
	}
	
}
